import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from './../components/Login.vue'
import Home from './../components/Home.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    // 进入首页重定向到欢迎
    children: [
      { path: '/welcome', component: () => import('../components/welcome.vue') },
      { path: '/users', component: () => import('../components/user/Users.vue') },
      { path: '/rights', component: () => import('../components/power/Rights.vue') },
      { path: '/roles', component: () => import('../components/power/Roles.vue') },
      { path: '/lists', component: () => import('../components/goods/List.vue') },
      { path: '/claspara', component: () => import('../components/goods/Claspara.vue') },
      { path: '/classification', component: () => import('../components/goods/Classification.vue') }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
